package com.hua.controller;

import com.hua.common.Response;
import com.hua.common.ResponseCode;
import com.hua.domain.OrderCommodity;
import com.hua.domain.Orders;
import com.hua.domain.domainVo.UserOrder;
import com.hua.domain.request.OrderRequest;
import com.hua.exception.BusinessException;
import com.hua.service.OrdersService;
import com.hua.util.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chenlong
 * @version jdk1.8
 * @dscription ProjectOfMilk OrderController
 * @date 2022/10/21 18:54
 */
@Api(value = "我们的订单相关的接口")
@RestController
@RequestMapping("/order")
public class OrderController {

    /**
     * 订单的service
     */
    @Resource
    private OrdersService orderService;

    /**
     * 查询我们自己的所有的订单
     */
    @GetMapping("/list/{userId}")
    @ApiOperation(value = "查询用户的历史订单")
    public Response<List<UserOrder>> list(@PathVariable("userId") Long useId) {
        //防止storeId  是空
        if (useId == null) {
            throw new BusinessException(ResponseCode.PARAMS_NULL_ERROR, "用户信息异常");
        }
        //查询用户有关的所有订单
        List<UserOrder> res = orderService.userOrderList(useId);
        if (res == null || res.isEmpty()) {
            throw new BusinessException(ResponseCode.DATA_NOT_FOUND, "没有找到该信息");
        }
        return ResultUtils.success(res);
    }

    /**
     * 生成订单 的外壳信息
     *
     * @param order 订单信息
     * @return 我们的订单id
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增订单信息 订单的外壳")
    public Response<Long> save(@RequestBody Orders order) {
        //防止锁定
        if (order.getUserId() == null || order.getUserId() <= 0) {
            throw new BusinessException(ResponseCode.PARAMS_NULL_ERROR, "用户未登录");
        }
        Long orderId = orderService.saveOrder(order);
        if (orderId == null) {
            throw new BusinessException(ResponseCode.UPDATE_ERROR, "生成订单失败");
        }
        return ResultUtils.success(orderId);
    }

    /**
     * 生成整个订单的完整信息
     */
    @ApiOperation(value = "保存我们的完整的订单信息")
    @PostMapping("/saveOrder")
    public Response<UserOrder> saveOrder(@RequestBody OrderRequest orderRequest) {
        //从请求体重拿出订单id和订单中的商品集合
        Long orderId = orderRequest.getOrderId();
        List<OrderCommodity> orderCommodityList = orderRequest.getOrderCommodityList();
        //防止参数有误
        if (orderId == null || orderCommodityList == null || orderCommodityList.isEmpty()) {
            throw new BusinessException(ResponseCode.PARAMS_NULL_ERROR, "下单失败");
        }
        UserOrder userOrder = orderService.saveOrderAddCommodity(orderId, orderCommodityList);
        //防止订单出错
        if (userOrder == null || userOrder.getOrders().getOrderId() <= 0 ||
                userOrder.getOrderCommodityList().isEmpty()) {
            throw new BusinessException(ResponseCode.UPDATE_ERROR, "保存订单失败");
        }

        return ResultUtils.success(userOrder, "下单成功!");
    }

    @ApiOperation(value = "删除单个订单")
    @DeleteMapping("/deleteOrder/{orderId}")
    public Response<Boolean> delete(@PathVariable(value = "orderId") String orderId){
        //防止orderId  是空
        if (orderId == null || orderId.equals("")) {
            throw new BusinessException(ResponseCode.SYSTEM_ERROR, "删除订单失败");
        }
        Boolean deleteRes = orderService.deleteOrder(orderId);
        return ResultUtils.success(deleteRes,"删除成功");
    }

    @ApiOperation(value="删除多个订单")
    @DeleteMapping("/deleteOrders")
    public Response<Boolean> deleteAll(@RequestBody List<Long> orderIds){
        //防止订单id有问题
        if(orderIds == null || orderIds.isEmpty()){
            throw new BusinessException(ResponseCode.PARAMS_ERROR,"删除失败");
        }
        Boolean res = orderService.deleteOrders(orderIds);

        return ResultUtils.success(res);
    }
}
